<?php

/**
 * @version     1.0.4
 * @package     com_hotels
 * @subpackage  mod_hotels
 * @copyright   Copyright (C) 2014. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Russell English <russell.english@stormit.co.uk> - http://www.stormit.co.uk
 */
defined('_JEXEC') or die;

// Get a db connection.
$db = JFactory::getDbo();

// Queries are separated - this will increase performance
// It has to be done this way, trust me :)

// Resort names
$query = $db->getQuery(true);
$query->select($db->quoteName(array('resortName')));
$query->from($db->quoteName('#__availability'));
$query->order('resortName ASC');
$query->group('resortName');
$db->setQuery($query);

// Load resorts
$resorts = $db->loadObjectList();
array_unshift($resorts, (object)array('resortName' => 'Any resort', 'value' => 'A'));

// Airports
$query = $db->getQuery(true);
$query->select($db->quoteName(array('dep')));
$query->from($db->quoteName('#__availability'));
$query->order('dep ASC');
$query->group('dep');
$db->setQuery($query);

// Load airports
$airports = $db->loadObjectList();
array_unshift($airports, (object)array('dep' => 'Any airport', 'value' => 'A'));

// Start dates
$query = $db->getQuery(true);
$query->select($db->quoteName(array('startDate')));
$query->from($db->quoteName('#__availability'));
$query->order('startDate ASC');
$query->group('startDate');
$db->setQuery($query);

// Load dates
$dates = $db->loadObjectList();

// Input
$input = JFactory::getApplication()->input;
?>

<div id="searchpanel">
	<div id="searchwrapper">
		<form action="<?php echo JRoute::_('index.php?&option=com_hotels&Itemid=104'); ?>" method="get">
			<div id="quicksearch">
				<label for="resort">Destination:</label>
				<select id="resort" name="resort">
					<?php $resortInput = $input->get('resort', 'A', 'string'); ?>
					<?php foreach ($resorts as $result): ?>
					<?php $value = isset($result->value) ? $result->value : $result->resortName; ?>
					<option value="<?php echo $value; ?>"<?php echo $resortInput == $value ? ' selected="selected"' : ''; ?>>
						<?php echo $result->resortName; ?>
					</option>
					<?php endforeach; ?>
				</select>
				<label for="airport">Departure Airport:</label>
				<select id="airport" name="airport">
					<?php $airportInput = $input->get('airport', 'A', 'string'); ?>
					<?php foreach ($airports as $result): ?>
					<?php $value = isset($result->value) ? $result->value : $result->dep; ?>
					<option value="<?php echo $value; ?>"<?php echo $airportInput == $value ? ' selected="selected"' : ''; ?>>
						<?php echo $result->dep; ?>
					</option>
					<?php endforeach; ?>
				</select>

				<label for="searchdate">Departure Date:</label>
				<select id="searchdate" name="searchdate">
					<option selected="selected" value="A">Choose Date</option>
					<?php $dateInput = $input->get('searchdate', null, 'string'); ?>
					<?php foreach ($dates as $result): ?>
					<option value="<?php echo $result->startDate; ?>"<?php echo $dateInput == $result->startDate ? ' selected="selected"' : ''; ?>>
						<?php echo $result->startDate; ?>
					</option>
					<?php endforeach; ?>
				</select>

				<span>No of Nights:</span><span class="nonights">7</span>
				<div class="passengers">
					<strong>Passengers:</strong>
					<p class="tbrow">
						<label class="adults" for="numadults">Adults:</label>
						<select id="numadults" class="adultsno" name="numadults">
							<?php $default = $input->get('numadults', 2, 'uint'); ?>
							<?php for($i = 1; $i <= 19; $i++): ?>
							<option value="<?php echo $i; ?>"<?php echo $i == $default ? ' selected=""' : '' ?>>
								<?php echo $i; ?>
							</option>
							<?php endfor; ?>
						</select>
					</p>
					<p class="tbrow">
						<label class="children" for="numchild">Children:</label>
						<select id="numchild" class="childrenno" name="numchild">
							<?php $default = $input->get('numchild', 0, 'uint'); ?>
							<?php for($i = 0; $i <= 19; $i++): ?>
							<option value="<?php echo $i; ?>"<?php echo $i == $default ? ' selected=""' : '' ?>>
								<?php echo $i; ?>
							</option>
							<?php endfor; ?>
						</select>
					</p>
					<p class="tbrow">
						<label class="infants" for="numinfants">Infants:</label>
						<select id="numinfants" class="infantsno" name="numinfants">
							<?php $default = $input->get('numinfants', 0, 'uint'); ?>
							<?php for($i = 0; $i <= 19; $i++): ?>
							<option value="<?php echo $i; ?>"<?php echo $i == $default ? ' selected=""' : '' ?>>
								<?php echo $i; ?>
							</option>
							<?php endfor; ?>
						</select>
					</p>
				</div>
				<br />
				<input class="searchbtn" type="submit" alt="search" value="Search" />
			</div>
		</form>
	</div>
</div>

